package com.gitee.huxing.course.conc.conc02.lock;

import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.conc.conc02.lock
 * @ClassName: Count
 * @Author: huxingmale
 * @DateTime: 2021-08-24 21:25
 */
public class Count2 {

    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    public void get(){
        lock.readLock().lock();
        try {
            System.out.println(Thread.currentThread().getName() + "get begin");
            Thread.sleep(1000L);
            System.out.println(Thread.currentThread().getName() + "get end");
        }catch (InterruptedException ex){
            ex.printStackTrace();
        } finally {
            lock.readLock().unlock();
        }
    }

    public void put(){
        lock.readLock().lock();
        try {
            System.out.println(Thread.currentThread().getName() + "put begin");
            Thread.sleep(1000L);
            System.out.println(Thread.currentThread().getName() + "put end");
        }catch (InterruptedException ex){
            ex.printStackTrace();
        } finally {
            lock.readLock().unlock();
        }
    }
}
